#pragma once

#include <algorithm>
#include <string>

inline void toLowerString(std::string& str)
{
    for (size_t i = 0; i < str.size(); i++)
    {
        if(str[i] > 'A' && str[i] <= 'Z')
        {
            str[i] = str[i] + ('a' - 'A');
        }
    }
}

class Field
{
public:
    enum DataTypes
    {
        DB_TYPE_UNKNOWN = 0x00,
        DB_TYPE_STRING  = 0x01,
        DB_TYPE_INTEGER = 0x02,
        DB_TYPE_FLOAT   = 0x03,
        DB_TYPE_BOOL    = 0x04
    };

    Field():m_type(DB_TYPE_UNKNOWN) {};
    
    Field(Field& f) 
    {
         m_strValue = f.m_strValue;
         m_strFieldName = f.m_strFieldName;
         m_type = f.m_type;
    }

    Field(const char* value, DataTypes type)
    {
        m_type = type;
        m_strValue = value;
    }

    ~Field();

    const DataTypes    getType()   const { return m_type; }
    const std::string  getString() const { return m_strValue; }
    const std::string& getName()   const { return m_strFieldName; }
    const bool         isValid() const { return m_bValid; }

    void setValid(bool status) { m_bValid = status; }
    void setType(DataTypes type) { m_type = type; }
    void setValue(const char* value,size_t uLen) { m_strValue.assign(value,uLen); }
    void setName(const std::string& strName) { m_strFieldName = strName; toLowerString(m_strFieldName);}

    float    getFloat()   const {return static_cast<float>(atof(m_strValue.c_str())); }
    bool     getBool()    const {return atoi(m_strValue.c_str())>0; }
    int32_t  getInt32()   const {return static_cast<int32_t> (atol(m_strValue.c_str()));}
    uint8_t  getUInt8()   const {return static_cast<uint8_t> (atol(m_strValue.c_str()));}
    uint16_t getUInt16()  const {return static_cast<uint16_t>(atol(m_strValue.c_str()));}
    int16_t  getInt16()   const {return static_cast<int16_t> (atol(m_strValue.c_str()));}
    uint32_t getUInt32()  const {return static_cast<uint32_t>(atol(m_strValue.c_str()));}
    uint64_t getUInt64()  const {return static_cast<uint64_t>(atol(m_strValue.c_str()));}


private:
    bool        m_bValid;
    std::string m_strValue;
    std::string m_strFieldName;
    DataTypes   m_type;
};